package com.chenchao.javafile;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/18 21:15
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javaFile.Test12.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 转换流
 */
public class Test12 {
    public static void main(String[] args) {
        net();
        net2();
        read();
    }

    /**
     * 键盘输入输出
     */
    public static void read() {

        String exit="exit";
        //关闭字符串
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
            //System.in/out是字节输入输出流
            String str = "";
            while (!(exit.equals(str))) {
                //当输入字符串（字节流）等于exit时退出
                str = reader.readLine();
                //光标移到下一行了
                writer.write(str);
                writer.newLine();
                //换行
                writer.flush();
                //刷新缓存，不然不显示

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 操作网络流，下载百度源码
     * 用字节转换流的字节进行解码
     */
    public static void net() {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader
                (new URL("https://www.baidu.com").openStream(), StandardCharsets.UTF_8));
             BufferedWriter writer = new BufferedWriter(
                     new OutputStreamWriter(new FileOutputStream("myjava01/src/fo/baidu.html"), StandardCharsets.UTF_8)))
        //默认字符集跟工程相关,
        {
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                System.out.println(str);
                writer.write(str);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字节流字节转字符流会因为字节不够导致乱码(中文三个字符，英文一个字符)
     */
    public static void net2() {
        try (InputStream inputStream = new URL("https://www.baidu.com").openStream()) {
            int i ;
            int j = 2;
            //j为标识符，标识读到中文字符还有几个字节，-1标识读完了
            int m = 0;
            //中文字符解码
            while ((i = inputStream.read()) != -1) {
                //[224,240)与[128,192)
                if (i < 240 && i >= 224|| i >= 128 && i < 192) {
                    //三个字节进行如下处理
                    i = i >= 224 ? i - 224 : i - 128;
                    //去掉标识用的二进制位
                    m += i << (6 * (j-- % 3));
                    //6个二进制进行拼接
                    if (j == -1) {
                        //中文字节读完后的操作
                        System.out.println((char) m);
                        m = 0;
                        j = 2;
                    }
                    continue;
                }
                System.out.println((char) i);
                //默认不是三个字节就是一个字节（不太严谨）
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
